package com.g1.controller;

import com.g1.entity.Food;
import com.g1.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/Food")
public class FoodController {
    @Autowired
    private FoodService foodService;
    //查询
    @RequestMapping("selectAll")
    public ModelAndView selectAll() {
        ModelAndView mav = new ModelAndView("foodList");
        List<Food> foods = foodService.list();
        mav.addObject("foods", foods);
        return mav;
    }
    //跳转增加页面
    @RequestMapping("addgo1")
    public String addgo1() {
        return "insertfood";
    }

    //增加
    @RequestMapping("addgo")
    public ModelAndView addgo(Food food) {
        foodService.add(food);
        ModelAndView mav = new ModelAndView("redirect:/Food/selectAll");
        return mav;
    }
    //跳转修改页面
    @RequestMapping("editgo")
    public ModelAndView  selectById(int id){
        ModelAndView modelAndView=new ModelAndView("updatefood");
        Food food =  foodService.get(id);
        modelAndView.addObject("food",food);
        return modelAndView;
    }
    //修改
    @RequestMapping("edit")
    public ModelAndView edit(Food food) {
        foodService.edit(food);
        ModelAndView mav = new ModelAndView("redirect:/Food/selectAll");
        return mav;
    }

    //删除
    @RequestMapping("delete")
    public ModelAndView delete(int id) {
        foodService.delete(id);
        ModelAndView mav = new ModelAndView("redirect:/Food/selectAll");
        return mav;
    }
}

